package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.ElectronicPendulumdetailDataBo;
import com.ruoyi.tb.lmt.domain.vo.ElectronicPendulumdetailDataVo;
import com.ruoyi.tb.lmt.domain.ElectronicPendulumdetailData;
import com.ruoyi.tb.lmt.mapper.ElectronicPendulumdetailDataMapper;
import com.ruoyi.tb.lmt.service.IElectronicPendulumdetailDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 电子摆详细数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@RequiredArgsConstructor
@Service
public class ElectronicPendulumdetailDataServiceImpl implements IElectronicPendulumdetailDataService {

    private final ElectronicPendulumdetailDataMapper baseMapper;

    /**
     * 查询电子摆详细数据
     */
    @Override
    public ElectronicPendulumdetailDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询电子摆详细数据列表
     */
    @Override
    public TableDataInfo<ElectronicPendulumdetailDataVo> queryPageList(ElectronicPendulumdetailDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ElectronicPendulumdetailData> lqw = buildQueryWrapper(bo);
        Page<ElectronicPendulumdetailDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电子摆详细数据列表
     */
    @Override
    public List<ElectronicPendulumdetailDataVo> queryList(ElectronicPendulumdetailDataBo bo) {
        LambdaQueryWrapper<ElectronicPendulumdetailData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicPendulumdetailData> buildQueryWrapper(ElectronicPendulumdetailDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicPendulumdetailData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtId()), ElectronicPendulumdetailData::getLmtId, bo.getLmtId());
        lqw.eq(StringUtils.isNotBlank(bo.getTestDate()), ElectronicPendulumdetailData::getTestDate, bo.getTestDate());
        lqw.eq(StringUtils.isNotBlank(bo.getReportNumber()), ElectronicPendulumdetailData::getReportNumber, bo.getReportNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDisplacementAndTheoreticalValueL()), ElectronicPendulumdetailData::getDisplacementAndTheoreticalValueL, bo.getDisplacementAndTheoreticalValueL());
        lqw.eq(StringUtils.isNotBlank(bo.getDisplacementAndTheoreticalValueU()), ElectronicPendulumdetailData::getDisplacementAndTheoreticalValueU, bo.getDisplacementAndTheoreticalValueU());
        lqw.eq(StringUtils.isNotBlank(bo.getMeasuredValuePositive()), ElectronicPendulumdetailData::getMeasuredValuePositive, bo.getMeasuredValuePositive());
        lqw.eq(StringUtils.isNotBlank(bo.getMeasuredValueReverse()), ElectronicPendulumdetailData::getMeasuredValueReverse, bo.getMeasuredValueReverse());
        lqw.eq(StringUtils.isNotBlank(bo.getLimitErrorPositive()), ElectronicPendulumdetailData::getLimitErrorPositive, bo.getLimitErrorPositive());
        lqw.eq(StringUtils.isNotBlank(bo.getLimitErrorReverse()), ElectronicPendulumdetailData::getLimitErrorReverse, bo.getLimitErrorReverse());
        lqw.eq(StringUtils.isNotBlank(bo.getLinearityPositive()), ElectronicPendulumdetailData::getLinearityPositive, bo.getLinearityPositive());
        lqw.eq(StringUtils.isNotBlank(bo.getLinearityReverse()), ElectronicPendulumdetailData::getLinearityReverse, bo.getLinearityReverse());
        lqw.eq(StringUtils.isNotBlank(bo.getGapError()), ElectronicPendulumdetailData::getGapError, bo.getGapError());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ElectronicPendulumdetailData::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, ElectronicPendulumdetailData::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), ElectronicPendulumdetailData::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, ElectronicPendulumdetailData::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增电子摆详细数据
     */
    @Override
    public Boolean insertByBo(ElectronicPendulumdetailDataBo bo) {
        ElectronicPendulumdetailData add = BeanUtil.toBean(bo, ElectronicPendulumdetailData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改电子摆详细数据
     */
    @Override
    public Boolean updateByBo(ElectronicPendulumdetailDataBo bo) {
        ElectronicPendulumdetailData update = BeanUtil.toBean(bo, ElectronicPendulumdetailData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicPendulumdetailData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电子摆详细数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
